<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    *{
      padding: 0;
      margin: 0;
    }
    header{
      width: 100%;
      line-height: 50px;
      background-color: antiquewhite;
      text-align: center;
    }
    section{
      width: 100%;
      height: 400px;
      background-color: burlywood;
    }
    /**
    * 方法一:粘性布局实现吸顶效果
    */
    .css-nav-bar{
      width: 100%;
      line-height: 50px;
      text-align: center;
      background-color: cadetblue;
      position: sticky;
      top:0px;
    }
    .js-nav-bar{
      width: 100%;
      line-height: 50px;
      text-align: center;
      background-color:darkkhaki;
    }
    .js-nav-bar-substitute{
      display: none;
      width: 100%;
      height: 50px;
    }
  </style>
</head>
<body>
  <header>头部导航</header>
  <section>
    中间内容
  </section>
  <div class='css-nav-bar'>
    css粘性布局实现吸顶效果的navbar
  </div>
  <section>
    列表内容1
  </section>
  <section>
    列表内容2
  </section>
  <div class='js-nav-bar'>
    js屏幕滑动时实现吸顶效果的navbar
  </div>
  <div class="js-nav-bar-substitute"></div>
  <section>
    列表内容3
  </section>
  <section>
    列表内容4
  </section>

  <script>
    var element = document.getElementsByClassName('js-nav-bar')[0]
    var elementTB = document.getElementsByClassName('js-nav-bar-substitute')[0]
    window.onscroll = (()=>{
      if(document.documentElement.scrollTop >= 1250){
        element.style.position = 'fixed'
        element.style.top = '50px'
        elementTB.style.display = 'block'
      }else {
        element.style.position = 'relative'
        element.style.top = 'auto'
        elementTB.style.display = 'none'
      }
    })
    
  </script>
</body>
</html>